<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Opens the module of the algorithm and the mode to be used</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.mcrypt-module-is-block-mode.html">mcrypt_module_is_block_mode</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.mcrypt-module-self-test.html">mcrypt_module_self_test</a></div>
 <div class="up"><a href="ref.mcrypt.html">Mcrypt 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.mcrypt-module-open" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">mcrypt_module_open</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.2, PHP 5)</p><p class="refpurpose"><span class="refname">mcrypt_module_open</span> &mdash; <span class="dc-title">Opens the module of the algorithm and the mode to be used</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.mcrypt-module-open-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">resource</span> <span class="methodname"><strong>mcrypt_module_open</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$algorithm</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$algorithm_directory</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$mode</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$mode_directory</code></span>
   )</div>

  <p class="para rdfs-comment">
   This function opens the module of the algorithm and the mode to be used.
   The name of the algorithm is specified in algorithm, e.g. <em>&quot;twofish&quot;</em> or is
   one of the <strong><code>MCRYPT_ciphername</code></strong> constants.  The module is closed by calling
   <span class="function"><a href="function.mcrypt-module-close.html" class="function">mcrypt_module_close()</a></span>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.mcrypt-module-open-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">algorithm</code></em></dt>

     <dd>

      <p class="para"><strong><code>MCRYPT_ciphername</code></strong> 常量中的一个，或者是字符串值的算法名称。</p>
     </dd>

    
    
     <dt>
<em><code class="parameter">algorithm_directory</code></em></dt>

     <dd>

      <p class="para">
       The <em><code class="parameter">algorithm_directory</code></em> parameter is used to locate
       the encryption module. When you supply a directory name, it is used.  When
       you set it to an empty string (<em>&quot;&quot;</em>), the value set by the
       <em>mcrypt.algorithms_dir</em> <var class="filename">php.ini</var> directive is used. When
       it is not set, the default directory that is used is the one that was compiled
       into libmcrypt (usually <var class="filename">/usr/local/lib/libmcrypt</var>).
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">mode</code></em></dt>

     <dd>

      <p class="para"><strong><code>MCRYPT_MODE_modename</code></strong> 常量中的一个，或以下字符串中的一个：&quot;ecb&quot;，&quot;cbc&quot;，&quot;cfb&quot;，&quot;ofb&quot;，&quot;nofb&quot; 和 &quot;stream&quot;。</p>
     </dd>

    
    
     <dt>
<em><code class="parameter">mode_directory</code></em></dt>

     <dd>

      <p class="para">
       The <em><code class="parameter">mode_directory</code></em> parameter is used to locate
       the encryption module. When you supply a directory name, it is used.  When
       you set it to an empty string (<em>&quot;&quot;</em>), the value set by the
       <em>mcrypt.modes_dir</em> <var class="filename">php.ini</var> directive is used. When
       it is not set, the default directory that is used is the one that was compiled-in
       into libmcrypt (usually <var class="filename">/usr/local/lib/libmcrypt</var>).
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.mcrypt-module-open-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Normally it returns an encryption descriptor, or <strong><code>FALSE</code></strong> on error.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.mcrypt-module-open-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-824">
    <p><strong>Example #1 <span class="function"><strong>mcrypt_module_open()</strong></span> Examples</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;&nbsp;&nbsp;$td&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mcrypt_module_open</span><span style="color: #007700">(</span><span style="color: #0000BB">MCRYPT_DES</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">MCRYPT_MODE_ECB</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'/usr/lib/mcrypt-modes'</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$td&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mcrypt_module_open</span><span style="color: #007700">(</span><span style="color: #DD0000">'rijndael-256'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'ofb'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   The first line in the example above will try to open the <em>DES</em> cipher from
   the default directory and the <em>ECB</em> mode from the directory
   <var class="filename">/usr/lib/mcrypt-modes</var>. The second example uses
   strings as name for the cipher and mode, this only works when the
   extension is linked against libmcrypt 2.4.x or 2.5.x.
  </p>

  <p class="para">
   <div class="example" id="example-825">
    <p><strong>Example #2 Using <span class="function"><strong>mcrypt_module_open()</strong></span> in encryption</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Open&nbsp;the&nbsp;cipher&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$td&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mcrypt_module_open</span><span style="color: #007700">(</span><span style="color: #DD0000">'rijndael-256'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'ofb'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Create&nbsp;the&nbsp;IV&nbsp;and&nbsp;determine&nbsp;the&nbsp;keysize&nbsp;length,&nbsp;use&nbsp;MCRYPT_RAND<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;on&nbsp;Windows&nbsp;instead&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$iv&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mcrypt_create_iv</span><span style="color: #007700">(</span><span style="color: #0000BB">mcrypt_enc_get_iv_size</span><span style="color: #007700">(</span><span style="color: #0000BB">$td</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">MCRYPT_DEV_RANDOM</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$ks&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mcrypt_enc_get_key_size</span><span style="color: #007700">(</span><span style="color: #0000BB">$td</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Create&nbsp;key&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">md5</span><span style="color: #007700">(</span><span style="color: #DD0000">'very&nbsp;secret&nbsp;key'</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$ks</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Intialize&nbsp;encryption&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">mcrypt_generic_init</span><span style="color: #007700">(</span><span style="color: #0000BB">$td</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$key</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$iv</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Encrypt&nbsp;data&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$encrypted&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mcrypt_generic</span><span style="color: #007700">(</span><span style="color: #0000BB">$td</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'This&nbsp;is&nbsp;very&nbsp;important&nbsp;data'</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Terminate&nbsp;encryption&nbsp;handler&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">mcrypt_generic_deinit</span><span style="color: #007700">(</span><span style="color: #0000BB">$td</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Initialize&nbsp;encryption&nbsp;module&nbsp;for&nbsp;decryption&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">mcrypt_generic_init</span><span style="color: #007700">(</span><span style="color: #0000BB">$td</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$key</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$iv</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Decrypt&nbsp;encrypted&nbsp;string&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$decrypted&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mdecrypt_generic</span><span style="color: #007700">(</span><span style="color: #0000BB">$td</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$encrypted</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Terminate&nbsp;decryption&nbsp;handle&nbsp;and&nbsp;close&nbsp;module&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">mcrypt_generic_deinit</span><span style="color: #007700">(</span><span style="color: #0000BB">$td</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">mcrypt_module_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$td</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Show&nbsp;string&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">trim</span><span style="color: #007700">(</span><span style="color: #0000BB">$decrypted</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.mcrypt-module-open-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.mcrypt-module-close.html" class="function" rel="rdfs-seeAlso">mcrypt_module_close()</a> - Closes the mcrypt module</span></li>
    <li class="member"><span class="function"><a href="function.mcrypt-generic.html" class="function" rel="rdfs-seeAlso">mcrypt_generic()</a> - This function encrypts data</span></li>
    <li class="member"><span class="function"><a href="function.mdecrypt-generic.html" class="function" rel="rdfs-seeAlso">mdecrypt_generic()</a> - Decrypts data</span></li>
    <li class="member"><span class="function"><a href="function.mcrypt-generic-init.html" class="function" rel="rdfs-seeAlso">mcrypt_generic_init()</a> - This function initializes all buffers needed for encryption</span></li>
    <li class="member"><span class="function"><a href="function.mcrypt-generic-deinit.html" class="function" rel="rdfs-seeAlso">mcrypt_generic_deinit()</a> - This function deinitializes an encryption module</span></li>
   </ul>
  </p>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.mcrypt-module-is-block-mode.html">mcrypt_module_is_block_mode</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.mcrypt-module-self-test.html">mcrypt_module_self_test</a></div>
 <div class="up"><a href="ref.mcrypt.html">Mcrypt 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
